

package ru.samgtu.wst.ui.swing;

import ru.samgtu.wst.model.Message;
import ru.samgtu.wst.model.NamedValue;
import ru.samgtu.wst.model.Preferences;

import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeEvent;


public class MessagePanel extends javax.swing.JPanel {

	private ContentPanel _cp;
	private HeaderPanel _hp;
	private Message _message = null;
	private boolean _editable = false;
	private boolean _modified = false;

	/** Creates new form MessagePanel */
	public MessagePanel() {
		initComponents();
		setName("Message");

		_hp = new HeaderPanel();
		messageSplitPane.setTopComponent(_hp);
		_cp = new ContentPanel();
		messageSplitPane.setBottomComponent(_cp);
		String dividerLocation = Preferences
				.getPreference("MessagePanel.dividerLocation");
		if (dividerLocation != null) {
			try {
				messageSplitPane.setDividerLocation(Integer
						.parseInt(dividerLocation));
			} catch (NumberFormatException nfe) {
			}
		}
		messageSplitPane
				.addPropertyChangeListener(new PropertyChangeListener() {
					public void propertyChange(PropertyChangeEvent e) {
						if (e.getPropertyName().equals("dividerLocation")) {
							Preferences.setPreference(
									"MessagePanel.dividerLocation", e
											.getNewValue().toString());
						}
					}
				});
		setEditable(false);
		setMessage(null);
	}

	public MessagePanel(int orientation) {
		this();
		messageSplitPane.setOrientation(orientation);
	}

	public void setEditable(boolean editable) {
		_editable = editable;
		_hp.setEditable(editable);
		_cp.setEditable(editable);
	}

	public void setMessage(Message message) {
		_modified = false;
		_message = message;

		if (message != null) {
			_hp.setHeaders(_message.getHeaders());
			byte[] content = message.getContent();
			_cp.setContentType(message.getHeader("Content-Type"));
			_cp.setContent(content);
		} else {
			_hp.setHeaders(null);
			_cp.setContentType(null);
			_cp.setContent(null);
		}
		revalidate();
	}

	public Message getMessage() {
		if (_editable) {
			if (_hp.isModified() && (_message != null)) {
				_message.setHeaders(_hp.getHeaders());
			}
			if (_cp.isModified()) {
				_message.setContent(_cp.getContent());
				if (_message.getHeader("Content-Length") != null) {
					_message.setHeader(new NamedValue("Content-Length", Integer
							.toString(_message.getContent().length)));
				}
			}
		}
		return _message;
	}

	public boolean isModified() {
		return _editable && (_hp.isModified() || _cp.isModified());
	}

	/**
	 * This method is called from within the constructor to initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is always
	 * regenerated by the Form Editor.
	 */
	private void initComponents() {// GEN-BEGIN:initComponents
		java.awt.GridBagConstraints gridBagConstraints;

		messageSplitPane = new javax.swing.JSplitPane();

		setLayout(new java.awt.BorderLayout());

		setPreferredSize(new java.awt.Dimension(400, 200));
		messageSplitPane.setOrientation(javax.swing.JSplitPane.VERTICAL_SPLIT);
		messageSplitPane.setResizeWeight(0.3);
		messageSplitPane.setContinuousLayout(true);
		messageSplitPane.setDoubleBuffered(true);
		messageSplitPane.setOneTouchExpandable(true);
		add(messageSplitPane, java.awt.BorderLayout.CENTER);

	}// GEN-END:initComponents

	public static void main(String[] args) {
		byte[] content = new byte[0];
		ru.samgtu.wst.model.Response response = new ru.samgtu.wst.model.Response();
		try {
			String resp = "/home/rogan/workspace/WST/test/data/index-resp";
			if (args.length == 1) {
				resp = args[0];
			}
			java.io.FileInputStream fis = new java.io.FileInputStream(resp);
			response.read(fis);
		} catch (Exception e) {
			e.printStackTrace();
			System.exit(0);
		}

		javax.swing.JFrame top = new javax.swing.JFrame("Message Pane");
		top.getContentPane().setLayout(new java.awt.BorderLayout());
		top.addWindowListener(new java.awt.event.WindowAdapter() {
			@Override
			public void windowClosing(java.awt.event.WindowEvent evt) {
				System.exit(0);
			}
		});

		javax.swing.JButton button = new javax.swing.JButton("GET");
		final MessagePanel mp = new MessagePanel();
		top.getContentPane().add(mp);
		top.getContentPane().add(button, java.awt.BorderLayout.SOUTH);
		button.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				System.out.println(mp.getMessage());
			}
		});
		// top.setBounds(100,100,600,400);
		top.pack();
		top.setVisible(true);
		try {
			mp.setEditable(true);
			mp.setMessage(response);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	// Variables declaration - do not modify//GEN-BEGIN:variables
	private javax.swing.JSplitPane messageSplitPane;
	// End of variables declaration//GEN-END:variables

}
